define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluedu/course_free/index' + location.search,
                    add_url: 'xiluedu/course_free/add',
                    edit_url: 'xiluedu/course_free/edit',
                    del_url: 'xiluedu/course_free/del',
                    multi_url: 'xiluedu/course_free/multi',
                    table: 'xiluedu_course_free',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

            //选择
            $(document).on('click','.course-choose',function () {
                var that = $(this);
                var url = that.attr('data-url') || 'xiluedu/course/selects';
                var course_ids_obj = $("[name^=course_ids]");
                var selectids = [];
                for (var i=0;i<course_ids_obj.length;i++) {
                    selectids[i] = $(course_ids_obj[i]).val();
                }
                parent.Fast.api.open(url + "?ids="+selectids+"&multiple=true", __('Choose'), {
                    callback: function (data) {
                        console.log(data);
                        var html = htmlTemplate(data.data);
                        $('.table tbody').html(html);
                    }
                });
                return false;
            });
            $(document).on('click','.course_detail',function () {
                var course_id = $(this).data('course_id');
                parent.Fast.api.open("xiluedu/course/edit/ids/"+course_id, __('详情'));
            })
            $(document).on('click','.course_del',function () {
                var that = $(this);
                Layer.confirm("确认删除",function (index2) {
                    that.parents("tr").remove();
                    Layer.close(index2);
                })
            })
        },
        edit: function () {
            Controller.api.bindevent();

            //选择
            $(document).on('click','.course-choose',function () {
                var that = $(this);
                var url = that.attr('data-url') || 'xiluedu/course/selects';
                var course_ids_obj = $("[name^=course_ids]");
                var selectids = [];
                for (var i=0;i<course_ids_obj.length;i++) {
                    selectids[i] = $(course_ids_obj[i]).val();
                }
                parent.Fast.api.open(url + "?ids="+selectids+"&multiple=true", __('Choose'), {
                    callback: function (data) {
                        console.log(data);
                        var html = htmlTemplate(data.data);
                        $('.table tbody').html(html);
                    }
                });
                return false;
            });
            $(document).on('click','.course_detail',function () {
                var course_id = $(this).data('course_id');
                parent.Fast.api.open("xiluedu/course/edit/ids/"+course_id, __('详情'));
            })
            $(document).on('click','.course_del',function () {
                var that = $(this);
                Layer.confirm("确认删除",function (index2) {
                    that.parents("tr").remove();
                    Layer.close(index2);
                })
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
function htmlTemplate(data) {
    var html = '';

    for(var i=0;i<data.length;i++){
        var img = Fast.api.fixurl(data[i].thumb_image);
        html += '<tr>';
        html += '<input type="hidden" name="course_ids[]" value="'+data[i].id+'">';
        html += '<td><img width="40px" src="'+img+'" alt=""></td>';
        html += '<td>'+data[i].name+'</td>';
        html += '<td><p><button type="button" class="btn btn-danger course_del" data-course_id="'+data[i].id+'"><i class="fa fa-trash"></i></button>';
        html += '<button type="button" class="btn btn-warning course_detail" data-course_id="'+data[i].id+'"><i class="fa fa-eye"></i></button></p></td>';
        html += '</tr>';
    }
    return html;
}